package com.ruoyi.mp.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.domain.Rescue;
import com.ruoyi.mp.service.IRescueService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.client.RestTemplate;

/**
 * 救援任务Controller
 * 
 * @author mp
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/rescue")
public class RescueController extends BaseController
{
    @Autowired
    private IRescueService rescueService;

    /**
     * 查询救援任务列表
     */
    @RequiresPermissions("mp:rescue:list")
    @GetMapping("/list")
    public TableDataInfo list(Rescue rescue)
    {
        startPage();
        List<Rescue> list = rescueService.selectRescueList(rescue);
        return getDataTable(list);
    }

    /**
     * 导出救援任务列表
     */
    @RequiresPermissions("mp:rescue:export")
    @Log(title = "救援任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rescue rescue)
    {
        List<Rescue> list = rescueService.selectRescueList(rescue);
        ExcelUtil<Rescue> util = new ExcelUtil<Rescue>(Rescue.class);
        util.exportExcel(response, list, "救援任务数据");
    }

    /**
     * 获取救援任务详细信息
     */
    @RequiresPermissions("mp:rescue:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(rescueService.selectRescueById(id));
    }

    /**
     * 新增救援任务
     */
    @RequiresPermissions("mp:rescue:add")
    @Log(title = "救援任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Rescue rescue)
    {
        return toAjax(rescueService.insertRescue(rescue));
    }

    @PostMapping("/add")
    public AjaxResult add1(@RequestBody Rescue rescue)
    {
        int result = rescueService.insertRescue(rescue); // 插入成功返回1
        if (result > 0) {
            return AjaxResult.success("操作成功", rescue.getId()); // 把 ID 返回到 data 字段
        } else {
            return AjaxResult.error("新增失败");
        }
    }


    /**
     * 修改救援任务
     */
    @RequiresPermissions("mp:rescue:edit")
    @Log(title = "救援任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Rescue rescue)
    {
        return toAjax(rescueService.updateRescue(rescue));
    }

    /**
     * 删除救援任务
     */
    @RequiresPermissions("mp:rescue:remove")
    @Log(title = "救援任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(rescueService.deleteRescueByIds(ids));
    }

    @GetMapping("/map/resolve")
    public String resolveAddress(@RequestParam double lat, @RequestParam double lng) {
        String key = "4X5BZ-NLCE5-5JHI7-ID3BH-2TD7J-QQF7J";
        String url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=0";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(url, String.class);
    }

}
